<template>

  <div class="login-container">

    <div class="login-logo">
      <img src="./images/login-logo.png" alt="">
      <span>莆田餐巴</span>
    </div>

    <el-row class="login-item" style="display: flex;justify-content: center;">
      <el-col :span="10" class="login-left">
        <div>
          <!--<h1 class="text-jumbo text-ginormous">
            Rzico Boot!
          </h1>
          Rzico Boot 是全国流行开源企业综合服务平台，其功能涵盖了：CRM、销售、采购、库存、电子商务、门店收银等一系列企业信息化需求。
          <h2>用户技术连接实体产业生态圈</h2>
          <h6>如想了解更多，请联系您的责任人</h6>
          <ul class="list-unstyled">
            <li>或者你可以去:</li>
            <li class="link-type">
              <router-link to="/dashboard">
                官方网站
              </router-link>
            </li>
            <li class="link-type">
              <a href="https://www.taobao.com/">帮助文档</a>
            </li>
            <li><a href="#" @click.prevent="dialogVisible=true">联系客服</a></li>
          </ul>-->
          <img class="login-banner" src="./images/login-banner.png" alt="">
        </div>
      </el-col>
      <el-col :span="10" class="login-right">
        <el-form style="" ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
                 label-position="left">

          <div class="title-container">
            <h3 class="title">
              {{ $t('login.title') }}
            </h3>
          </div>

          <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              :placeholder="$t('手机号/邮箱')"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>

          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password"/>
          </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                :placeholder="$t('login.password')"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
          </span>
            </el-form-item>
          </el-tooltip>

          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-input type="text" placeholder="请输入验证码" class="verifyCode" v-model="loginForm.verifyCode"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11" :offset="1">
              <div>
                <div class="codeImg" @click="createCaptcha">
                  <img :src="codeImg" width="100%" height="100%" alt="">
                </div>
              </div>
            </el-col>
          </el-row>

          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:20px;"
                     @click.native.prevent="handleLogin">
            {{ $t('login.logIn') }}
          </el-button>


          <el-row justify="center">
            <el-col :span="8" align="center">
              <a class="label-button" @click="forget">
                <svg-icon icon-class="password"/>
                <span>&nbsp忘记密码</span></a>
            </el-col>
            <!-- <el-col :offset="8" :span="8" align="center">
               <a class="label-button" @click="register"><svg-icon icon-class="user" /><span>&nbsp注册账号</span></a>
             </el-col>-->
          </el-row>

          <!--<div style="position:relative;margin-top:5px;">-->
          <!--<div class="tips">-->
          <!--<span>{{ $t('login.registerTips') }} </span>-->
          <!--</div>-->
          <!--&lt;!&ndash;<el-button class="thirdparty-button" type="primary">&ndash;&gt;-->
          <!--&lt;!&ndash;{{ $t('login.register') }}&ndash;&gt;-->
          <!--&lt;!&ndash;</el-button>&ndash;&gt;-->
          <!--</div>-->

        </el-form>
      </el-col>
    </el-row>

    <div class="login-bottom ">
      <p>用技术连接和重构实体产业生态圈</p>
      <p>Copyright ©2020 福建省莆田餐巴科技有限公司 All Rights Reserved. 闽ICP备13006726号</p>
    </div>

  </div>
</template>

<script>

  import {validUsername} from '@/utils/validate'
  import utils from '../../utils/utils.js'
  import ElRow from "element-ui/packages/row/src/row";
  import ElCol from "element-ui/packages/col/src/col";

  export default {
    components: {
      ElCol,
      ElRow
    },
    name: 'Login',
    data() {
      const validatePassword = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('密码不能少于6位'))
        } else {
          callback()
        }
      }
      return {
        loginForm: {
          username: '',
          password: '',
          verifyCode: '',
          verifyCodeKey: ''
        },
        loginRules: {
          username: [{required: true, trigger: 'blur'}],
          password: [{required: true, trigger: 'blur', validator: validatePassword}],
          verifyCode: [{required: true, trigger: 'blur'}]
        },
        passwordType: 'password',
        capsTooltip: false,
        loading: false,
        redirect: undefined,
        otherQuery: {}
      }
    },
    watch: {
      $route: {
        handler: function (route) {
          const query = route.query
          if (query) {
            this.redirect = query.redirect
            this.otherQuery = this.getOtherQuery(query)
          }
        },
        immediate: true
      }
    },
    created() {
      this.loginForm.verifyCodeKey = utils.getUUID()
      this.createCaptcha()
      // window.addEventListener('storage', this.afterQRScan)
    },
    mounted() {
      if (this.loginForm.username === '') {
        this.$refs.username.focus()
      } else if (this.loginForm.password === '') {
        this.$refs.password.focus()
      }
    },
    destroyed() {
      // window.removeEventListener('storage', this.afterQRScan)
    },
    methods: {
      forget() {
        this.$router.push('/password')
      },
      register() {
        this.$router.push('/register')
      },
      // 图片验证码
      createCaptcha() {
        var _this = this
        _this.codeImg = process.env.VUE_APP_BASE_API + '/user/getVerifyCode?verifyCodeKey=' + _this.loginForm.verifyCodeKey + '&date=' + new Date().getTime()
      },
      checkCapslock(e) {
        const {key} = e
        this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
      },
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/login', this.loginForm)
              .then(() => {
                console.log('login success!!')
                this.createCaptcha()
                this.$router.push({path: '/', query: this.otherQuery})
                this.loading = false
              })
              .catch(() => {
                console.log('login error!!')
                this.createCaptcha()
                this.loading = false
              })
          } else {
            console.log('error submit!!')
            this.createCaptcha()
            this.loading = false
            return false
          }
        })
      },
      getOtherQuery(query) {
        return Object.keys(query).reduce((acc, cur) => {
          if (cur !== 'redirect') {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
      }
      // afterQRScan() {
      //   if (e.key === 'x-admin-oauth-code') {
      //     const code = getQueryObject(e.newValue)
      //     const codeMap = {
      //       wechat: 'code',
      //       tencent: 'code'
      //     }
      //     const type = codeMap[this.auth_type]
      //     const codeName = code[type]
      //     if (codeName) {
      //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
      //         this.$router.push({ path: this.redirect || '/' })
      //       })
      //     } else {
      //       alert('第三方登录失败')
      //     }
      //   }
      // }
    }
  }
</script>

<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg: #000; //#283443;

  $light_gray: #2d3a4b;
  $cursor: #fff;

  @supports (-webkit-mask: none) and (not (cater-color: #000)) {
    .login-container .el-input input {
      color: #000;
    }
  }


  /* reset element-ui style */
  .login-container {
    position: relative;
    // background: #dfe6ec;
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: #fff;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        //caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px #fff inset !important;
          -webkit-text-fill-color: #000 !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(0, 0, 0, 0.1);
      //background:$bg;
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style lang="scss" scoped>
  //$bg:#2d3a4b;
  //$bg:#fff;
  $bg: #fafafa;
  $dark_gray: #889aa4;
  $light_gray: #2d3a4b;

  .login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .login-logo {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 405px;
      height: 100px;
      padding: 50px 40px 10px 40px;
      justify-content: flex-start;
      img {
        width: 60px;
        height: 60px;
      }

      span {
        margin-left: 20px;
        font-size: 30px;
        font-weight: bold;
      }
    }

    .login-left {
      width: 400px;
      height: 400px;

      .login-banner {
        width: 100%;
      }
    }

    .login-right {
      width: 400px;
      height: 400px;
      padding: 0 50px;
      position: relative;
      background: #fff;
    }

    .login-form {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 320px;
      /*max-width: 100%;*/
      //padding: 120px 0px 0;
      margin: 0 0;
      overflow: hidden;
    }

    .label-button {
      border-bottom: 0px solid #dfe6ec;
      padding-bottom: 10px;
      margin: 10px auto;
      font-weight: bold;
      color: $light_gray;
    }

    .tips {
      margin-bottom: 20px;
      font-size: 14px;
      color: #fff;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .codeImg {
      height: 49px;
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 20px;
        color: $light_gray;
        margin: 0px auto 20px auto;
        text-align: center;
        /*font-weight: bold;*/
      }

      .set-language {
        color: #fff;
        position: absolute;
        top: 3px;
        font-size: 18px;
        right: 0px;
        cursor: pointer;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .thirdparty-button {
      position: absolute;
      right: 0;
      bottom: 0px;
    }

    @media only screen and (max-width: 470px) {
      .thirdparty-button {
        display: none;
      }
      .thirdparty-button {
        display: none;
      }
    }


    .pan-back-btn {
      background: #008489;
      color: #fff;
      border: none !important;
    }

    .pan-gif {
      margin: 0 auto;
      display: block;
    }

    .pan-img {
      display: block;
      margin: 0 auto;
      width: 100%;
    }

    .text-jumbo {
      font-size: 60px;
      font-weight: 700;
      color: #484848;
    }

    .list-unstyled {
      font-size: 14px;

      li {
        padding-bottom: 5px;
      }

      a {
        color: #008489;
        text-decoration: none;

        &:hover {
          text-decoration: underline;
        }
      }
    }

    .login-bottom {
      /* margin-top: 4%;
       display: flex;
       justify-content: center;
       align-items: center;
       flex-direction: column;*/
      /*position: absolute;
      display: block;
      width: 100%;
      bottom: 4%;*/
      line-height: 30px;
      text-align: center;
      margin-bottom: 30px;

      p:nth-child(1) {
        font-size: 17px;
        color: $light_gray;
      }

      p:nth-child(2) {
        font-size: 10px;
        color: $light_gray;
      }
    }
  }

</style>
